"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_api = require("../../utils/api.js");
const _sfc_main = {
  __name: "detail",
  setup(__props) {
    const tabs = ["章节", "详情", "评价"];
    const currentTab = common_vendor.ref(0);
    const courseDetail = common_vendor.ref({});
    const currentChapterIndex = common_vendor.ref(null);
    common_vendor.ref("");
    const switchTab = (index) => {
      currentTab.value = index;
    };
    const swiperChange = (e) => {
      currentTab.value = e.detail.current;
    };
    const enrollCourse = async () => {
      try {
        const res = await utils_api.courseApi.enrollCourse({
          id: courseDetail.value.id
        });
        if (res.code === 0) {
          common_vendor.index.showToast({
            title: "报名成功",
            icon: "success"
          });
        }
      } catch (error) {
        console.error("报名失败:", error);
        common_vendor.index.showToast({
          title: "请先登录",
          icon: "none"
        });
      }
    };
    const getChapterContent = async (courseId, chapter) => {
      try {
        const res = await utils_api.courseApi.getChapterContent(courseId, chapter);
        if (res.code === 0) {
          if (!res.data.content || res.data.content.trim() === "") {
            if (!res.data.canAccess) {
              common_vendor.index.showModal({
                title: "提示",
                content: "请先登录",
                showCancel: false
              });
            } else {
              common_vendor.index.showModal({
                title: "提示",
                content: "请先登录",
                success: (res2) => {
                  if (res2.confirm) {
                    common_vendor.index.navigateTo({
                      url: "/pages/user/login"
                    });
                  }
                }
              });
            }
            return null;
          }
          return res.data;
        }
      } catch (error) {
        console.error("获取章节内容失败:", error);
        common_vendor.index.showToast({
          title: "获取章节内容失败",
          icon: "none"
        });
        return null;
      }
    };
    const goToChapter = async (chapter, index) => {
      try {
        const chapterData = await getChapterContent(courseDetail.value.id, index + 1);
        if (chapterData) {
          currentChapterIndex.value = index;
          common_vendor.index.setStorageSync("currentChapter", {
            ...chapter,
            ...chapterData,
            index: index + 1,
            courseTitle: courseDetail.value.title
          });
          common_vendor.index.setStorageSync("currentCourse", courseDetail.value);
          common_vendor.index.navigateTo({
            url: `/pages/course/chapter?id=${courseDetail.value.id}&chapter=${index + 1}`
          });
        }
      } catch (error) {
        console.error("获取章节内容失败:", error);
      }
    };
    const getCourseDetail = async (id) => {
      var _a;
      try {
        let res;
        res = await common_vendor.index.request({
          url: "http://localhost:8080/api/course/get",
          method: "GET",
          data: { id },
          header: {
            "content-type": "application/json"
          }
        });
        console.log("小程序请求结果:", res);
        if (res.statusCode === 200 && res.data.code === 0) {
          courseDetail.value = res.data.data;
          common_vendor.index.setStorageSync("currentCourse", res.data.data);
        } else {
          throw new Error(((_a = res.data) == null ? void 0 : _a.message) || "获取课程详情失败");
        }
      } catch (error) {
        console.error("获取课程详情失败:", error);
        common_vendor.index.showToast({
          title: error.message || "获取课程详情失败",
          icon: "none"
        });
      }
    };
    common_vendor.onMounted(() => {
      var _a, _b, _c;
      try {
        const query = common_vendor.index.getLaunchOptionsSync().query;
        const pages = getCurrentPages();
        const currentPage = pages[pages.length - 1];
        let courseId = null;
        if (currentPage && currentPage.options) {
          courseId = currentPage.options.id;
        } else if (currentPage && currentPage.$page && currentPage.$page.options) {
          courseId = currentPage.$page.options.id;
        } else if (query.id) {
          courseId = query.id;
        }
        console.log("小程序端获取到的参数:", {
          currentPage,
          options: currentPage == null ? void 0 : currentPage.options,
          $page: currentPage == null ? void 0 : currentPage.$page,
          query,
          courseId
        });
        if (courseId) {
          getCourseDetail(courseId);
          let chapterIndex = null;
          if ((_a = currentPage == null ? void 0 : currentPage.options) == null ? void 0 : _a.chapter) {
            chapterIndex = parseInt(currentPage.options.chapter) - 1;
          } else if ((_c = (_b = currentPage == null ? void 0 : currentPage.$page) == null ? void 0 : _b.options) == null ? void 0 : _c.chapter) {
            chapterIndex = parseInt(currentPage.$page.options.chapter) - 1;
          } else if (query.chapter) {
            chapterIndex = parseInt(query.chapter) - 1;
          }
          if (!isNaN(chapterIndex)) {
            currentChapterIndex.value = chapterIndex;
          }
        } else {
          throw new Error("课程信息不存在");
        }
      } catch (error) {
        console.error("获取参数失败:", error);
        common_vendor.index.showToast({
          title: error.message || "获取课程信息失败",
          icon: "none"
        });
        setTimeout(() => {
          common_vendor.index.navigateBack();
        }, 1500);
      }
    });
    return (_ctx, _cache) => {
      return {
        a: courseDetail.value.image,
        b: common_vendor.t(courseDetail.value.title),
        c: common_vendor.t(courseDetail.value.studentCount || 0),
        d: common_vendor.f(tabs, (tab, index, i0) => {
          return {
            a: common_vendor.t(tab),
            b: index,
            c: common_vendor.n({
              active: currentTab.value === index
            }),
            d: common_vendor.o(($event) => switchTab(index), index)
          };
        }),
        e: common_vendor.f(courseDetail.value.chapters, (chapter, index, i0) => {
          return {
            a: common_vendor.t(index + 1),
            b: common_vendor.t(chapter.title),
            c: common_vendor.t(chapter.duration),
            d: index,
            e: common_vendor.o(($event) => goToChapter(chapter, index), index)
          };
        }),
        f: courseDetail.value.description,
        g: common_vendor.f(courseDetail.value.comments, (comment, index, i0) => {
          return {
            a: comment.userAvatar,
            b: common_vendor.t(comment.userName),
            c: common_vendor.t(comment.createTime),
            d: common_vendor.t(comment.content),
            e: index
          };
        }),
        h: currentTab.value,
        i: common_vendor.o(swiperChange),
        j: common_vendor.t(courseDetail.value.price === 0 ? "免费" : `¥${courseDetail.value.price}`),
        k: common_vendor.o(enrollCourse)
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-3d21314d"]]);
wx.createPage(MiniProgramPage);
